package cn.com.note.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.blankj.utilcode.util.SizeUtils;

import cn.com.note.R;
import cn.com.note.view.sanckbar.Prompt;
import cn.com.note.view.sanckbar.TSnackBar;

public class SnackBarActivity extends AppCompatActivity implements View.OnClickListener {
    private TSnackBar snackBar;
    private int APP_DOWn = TSnackBar.APPEAR_FROM_TOP_TO_DOWN;
    private ViewGroup viewGroup, rootView;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_snackbar);

        initView();
    }

    private void initView() {
        rootView = (ViewGroup) findViewById(android.R.id.content).getRootView();
        radioGroup = findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_top:
                        APP_DOWn = TSnackBar.APPEAR_FROM_TOP_TO_DOWN;
                        viewGroup = rootView;
                        break;
                    case R.id.rb_bottom:
                        APP_DOWn = TSnackBar.APPEAR_FROM_BOTTOM_TO_TOP;
                        viewGroup = radioGroup;
                        break;
                }
            }
        });
        viewGroup = rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.success:
                snackBar = TSnackBar.make(viewGroup, "成功...", TSnackBar.LENGTH_SHORT, APP_DOWn)
                        .setAction("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        }).setPromptThemBackground(Prompt.SUCCESS);
                setMargin();
                snackBar.show();
                break;
            case R.id.error:
                snackBar = TSnackBar.make(viewGroup, "错误...", TSnackBar.LENGTH_LONG, APP_DOWn);
                snackBar.addIcon(R.mipmap.ic_launcher, 100, 100);
                snackBar.setAction("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                snackBar.setPromptThemBackground(Prompt.ERROR);
                setMargin();
                snackBar.show();
                break;
            case R.id.warning:
                snackBar = TSnackBar.make(viewGroup, "警告...", TSnackBar.LENGTH_LONG, APP_DOWn);
                snackBar.addIcon(R.mipmap.ic_launcher, 100, 100);
                snackBar.setAction("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                snackBar.setPromptThemBackground(Prompt.WARNING);
                setMargin();
                snackBar.show();
                break;
            case R.id.loginSuccess:
                snackBar = TSnackBar.make(viewGroup, "正在登录，请稍后...", TSnackBar.LENGTH_INDEFINITE, APP_DOWn);
                snackBar.setAction("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                snackBar.setPromptThemBackground(Prompt.SUCCESS);
                snackBar.addIconProgressLoading(0, true, false);
                setMargin();
                snackBar.show();
                mHandler.postDelayed(gotoLoginActSuccess, 5000);
                break;
            case R.id.loginFail:
                snackBar = TSnackBar.make(viewGroup, "正在登录，请稍后...", TSnackBar.LENGTH_INDEFINITE, APP_DOWn);
                snackBar.setAction("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                snackBar.setPromptThemBackground(Prompt.SUCCESS);
                snackBar.addIconProgressLoading(0, true, false);
                setMargin();
                snackBar.show();
                mHandler.postDelayed(gotoLoginActFail, 5000);
                break;
            default:

        }
    }

    private void setMargin() {
        if(APP_DOWn == TSnackBar.APPEAR_FROM_BOTTOM_TO_TOP){
            if(snackBar!=null){
                snackBar.setMargin(SizeUtils.dp2px(10));
            }
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (snackBar != null) {
                        snackBar.setPromptThemBackground(Prompt.SUCCESS).setText("登录成功").setDuration(TSnackBar.LENGTH_LONG).show();
                    }
                    break;
                case 1:
                    if (snackBar != null) {
                        snackBar.setPromptThemBackground(Prompt.ERROR).setText("登录失败").setDuration(TSnackBar.LENGTH_LONG).show();
                    }
                    break;
                default:
                    break;
            }
        }
    };
    ;
    Runnable gotoLoginActSuccess = new Runnable() {
        @Override
        public void run() {
            Message msg = new Message();
            msg.what = 0;
            mHandler.sendMessage(msg);
        }
    };
    Runnable gotoLoginActFail = new Runnable() {
        @Override
        public void run() {
            Message msg = new Message();
            msg.what = 1;
            mHandler.sendMessage(msg);
        }
    };
}
